package com.babyshop.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.babyshop.domain.Order;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class OrderDaoImpl implements OrderDao {

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void saveOfUpdate(Order order) {
		try {
			session.saveOrUpdate(order);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public void delete(int id) {
		try {
			Order order = (Order) session.get(Order.class, id);
			session.delete(order);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public Order get(int id) {
		Order order = null;
		try {
			order = (Order) session.get(Order.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return order;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Order> getAll() {
		List<Order> listOrders = null;
		try {
			listOrders = session.createQuery("from Order").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listOrders;
	}

}
